package com.qiangesoft.blog.controller.web;

import cn.dev33.satoken.stp.StpUtil;
import com.qiangesoft.blog.common.result.ResponseResult;
import com.qiangesoft.blog.system.pojo.dto.PasswordDTO;
import com.qiangesoft.blog.system.pojo.dto.UserAuthDTO;
import com.qiangesoft.blog.system.pojo.vo.SysUserDetailVO;
import com.qiangesoft.blog.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author qiange
 */
@RequestMapping("/web/user")
@RestController
@Api(tags = "用户管理")
@RequiredArgsConstructor
public class WebSysUserController {

    private final ISysUserService sysUserService;

    @ApiOperation("获取个人信息")
    @GetMapping
    public SysUserDetailVO getUserInfo() {
        return sysUserService.getUserById(StpUtil.getLoginIdAsLong());
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public ResponseResult logout() {
        StpUtil.logout(StpUtil.getLoginId());
        return ResponseResult.success("退出成功");
    }

    @ApiOperation("修改个人信息")
    @PutMapping
    public ResponseResult updateUser(@Validated @RequestBody UserAuthDTO userAuth) {
        sysUserService.updateUser(userAuth);
        return ResponseResult.success("信息修改成功");
    }

    @ApiOperation("修改密码")
    @PutMapping("/password")
    public ResponseResult updatePassword(@Validated @RequestBody PasswordDTO passwordDTO) {
        sysUserService.updatePassword(passwordDTO);
        return ResponseResult.success("信息成功");
    }
}

